package org.suyafei.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.suyafei.controller.request.LinkGroupAddRequest;
import org.suyafei.controller.request.LinkGroupUpdateRequest;
import org.suyafei.interceptor.LoginInterceptor;
import org.suyafei.manager.LinkGroupManager;
import org.suyafei.model.LinkGroupDO;
import org.suyafei.service.LinkGroupService;
import org.suyafei.vo.LinkGroupVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 宿某人
 * @Date: 2021/12/17/5:10 下午
 * @Description:
 */
@Service
@Slf4j
public class LinkGroupServiceImpl implements LinkGroupService {

    @Autowired
    private LinkGroupManager linkGroupManager;

    @Override
    public int add(LinkGroupAddRequest addRequest) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(addRequest.getTitle());
        linkGroupDO.setAccountNo(accountNo);

        int rows = linkGroupManager.add(linkGroupDO);
        return rows;
    }

    @Override
    public int del(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        int rows = linkGroupManager.del(groupId, accountNo);
        return rows;
    }

    @Override
    public LinkGroupVO detail(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId, accountNo);
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO, linkGroupVO);

        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> listAllLinkGroup() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        List<LinkGroupDO> linkGroupDOList = linkGroupManager.listAllLinkGroup(accountNo);
        List<LinkGroupVO> linkGroupVOList = linkGroupDOList.stream().map(item -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(item, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());
        return linkGroupVOList;
    }

    @Override
    public int updateLinkGroup(LinkGroupUpdateRequest request) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setId(request.getId());
        linkGroupDO.setTitle(request.getTitle());
        int rows = linkGroupManager.updateById(linkGroupDO);
        return rows;
    }
}
